Explain different accessibility modifiers and how many are there in C# with examples.
Explain different accessibility modifiers and how many are there in C# with examples.
407
14-Apr-2023
Aryan Kumar
24-Apr-2023In C#, there are five accessibility modifiers: public, private, protected, internal, and protected internal. These modifiers control the level of access that a class, method, property, or field has to other classes or code within the same assembly or outside of it.
Public: A public member is accessible from any code within the assembly or outside of it. This modifier is commonly used for methods, properties, and fields that need to be accessed by other code.
Private: A private member is only accessible from within the same class or struct. This modifier is used to encapsulate implementation details and hide them from other code.
Example:
Protected: A protected member is accessible from within the same class or struct, as well as any derived class. This modifier is used to expose implementation details to derived classes while still encapsulating them from other code.
Example:
Internal: An internal member is accessible from any code within the same assembly, but not from code outside of it. This modifier is used to expose implementation details to other code within the same assembly.
Example:
Protected internal: A protected internal member is accessible from within the same assembly, as well as any derived class whether inside or outside of the assembly. This modifier is used to expose implementation details to derived classes within the same assembly, as well as to other code within the same assembly.
Example: